
try:
    file = open('s_data.txt', 'r')
    s = eval(file.read())
    file.close()
except:
    s = []

while True:
    print("""
    ************************
    *    1: 添加学生        *
    *    2: 删除学生        *
    *    3: 修改学生        *
    *    4: 查询学生信息    *
    *    5: 查询所有学生信息*
    *    6: 保存信息        *
    *    7: 退出系统        *
    *************************
    """, end="")
    num = int(input("请输入功能序号:"))
    if num == 1:
        name = str(input("name"))
        code = int(input("code"))
        age = str(input("age"))
        for i in range(len(s)):
            if code == s[i]["code"]:
                print('该学生已存在')
                break
        else:
            s.append({"name": name, "code": code, 'age': age})
            print(f'添加{name}学生成功')
    elif num == 2:
        code = int(input("code"))
        for i in range(len(s)):
            if code == s[i]["code"]:
                name = s[i]['name']
                s.pop(i)
                print(f'删除{name}成功')
                break
        else:
            print("查无此人")
    elif num == 3:
        code = int(input("请输入学号："))
        for i in range(len(s)):
            if code == s[i]["code"]:
                name = input("请输入姓名")
                age = input("请输入年龄")
                s[i]["name"] = name
                s[i]["age"] = age
                break
        else:
            print("该学生不存在")
    elif num == 4:
        code = int(input("请输入学号："))
        for i in range(len(s)):
            if code == s[i]["code"]:
                print(s[i])
                break
        else:
            print("该学生不存在")
    elif num == 5:
        print(s)
    elif num == 6:
        i = open("s_data.txt", "w+")
        i.write(str(s))
        i.close()
        print('保存成功')
    else:
        break
print("系统结束")